import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/utils/toast.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  // 公告详情
  Map _announceDetail = {};

  // 获取公告详情
  getAnnounceDetail(String id) async {
    try {
      final res = await getAnnounceDetailAPI(id);
      setState(() {
        _announceDetail = res ?? {};
      });
    } catch (e) {
      PromptAction.showError(e.toString());
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 从路由参数中获取id
    final id = ModalRoute.of(context)?.settings.arguments as String?;
    if (id != null) {
      getAnnounceDetail(id);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('通知详情'),
      ),
      body: getBody(),
    );
  }

  // 构建页面主体
  Widget getBody() {
    return _announceDetail.isEmpty
        ? const Center(
            child: CircularProgressIndicator(),
          )
        : ListView(
            scrollDirection: Axis.vertical,
            children: [
              Container(
                  padding: const EdgeInsets.all(10),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(_announceDetail["title"] ?? "", style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
                      const SizedBox(height: 10),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            _announceDetail["creatorName"] ?? "",
                            style: const TextStyle(color: Colors.grey, fontSize: 14),
                          ),
                          Text(
                            _announceDetail["createdAt"] ?? "",
                            style: const TextStyle(color: Colors.grey, fontSize: 14),
                          )
                        ],
                      ),
                      const SizedBox(height: 20),
                      // 富文本组件
                      Html(data: _announceDetail["content"] ?? "")
                    ],
                  ))
            ],
          );
  }
}
